const { BaseServer } = require('../Base')

class PublicData extends BaseServer {
  constructor() {
    super()
    this.cityList = []
  }
  // 获取城市列表json
  getCityList() {
    if (!this.cityList.length) {
      this.cityList = require('../../public/data/cityList.json')
    }
    return this.cityList
  }
  // 富文本数据
  richtext(ctx) {
    const type = ctx.query.type
    let htmlPath = this.path.resolve(
      __dirname,
      '../../public/data/rich-text.html'
    )
    let textdata = this.tools.file.readFileSync(htmlPath)
    textdata = textdata
      .replace(/\$\{app\.serverPath\}/g, this.serverPath)
      .replace(/\r\n/g, '')
    if (type) {
      return textdata.replace(
        /<(img|video)/gi,
        '<$1 style="max-width:100%;height:auto"'
      )
    }
    return textdata
  }
  // 城市列表
  getArea(ctx) {
    const pid = ctx.query.pid || 0
    return this.getCityList().filter((item) => item.pid == pid)
  }
  // 获取所有城市列表[树状]
  getAreaAll() {
    return this.tools.jcore.toTree(this.getCityList())
  }
}

const ex = new PublicData()
module.exports = ex
